package com.gxa.modules.sys.controller;

import com.gxa.common.utils.ErrorCode;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.sys.dto.UserDto;
import com.gxa.modules.sys.dto.UserUpdateDto;
import com.gxa.modules.sys.dto.UsersDto;
import com.gxa.modules.sys.entity.User;
import com.gxa.modules.sys.form.UserForm;
import com.gxa.modules.sys.service.UserService;
import com.gxa.modules.sys.service.UserTokenService;
import io.swagger.annotations.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户接口")
@RestController
//@Slf4j
public class UserController {

    @Autowired
    private UserTokenService userTokenService;

    @Autowired
    private UserService userService;
    @ApiOperation(value="用户分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "name",value ="查询条件",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "id",value ="查询条件",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "tel",value ="查询条件",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="查询条件",dataType ="Date"),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="查询条件",dataType ="Date"),
            @ApiImplicitParam(paramType = "query",name = "order",value ="升序asc，降序填desc",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "sidx",value ="排序字段",dataType ="String"),

    })
    @GetMapping("/user")
    public Result<PageUtils> queryUser(@RequestParam @ApiIgnore Map<String,Object> params){
//        log.info("----params-----{}----",params);
        PageUtils pageUtils = this.userService.queryByPage(params);
        try {
            return new Result<PageUtils>().ok(pageUtils);
        } catch (Exception e) {
            return new Result().error("查询失败");
        }
    }

    @ApiOperation(value = "用户登录接口")
    @PostMapping("/user/login")
    public Result UserLogin(@RequestBody UserForm userForm){

        //1、拿着用户名去 查询用户信息
        User user = this.userService.queryByName(userForm.getName());
        if(user == null){
            return new Result().error(ErrorCode.ACCOUNT_PASSWORD_ERROR,"用户名或密码不正确");
        }

        //2、通过明文加密  对比  密文 是否一致
        String pwd = new SimpleHash("MD5", userForm.getPwd(), user.getSalt(), 1024).toString();
        //3、不一致      返回Result.error()
        if(!pwd.equals(user.getPwd())){
            return new Result().error(ErrorCode.ACCOUNT_PASSWORD_ERROR,"用户名或密码不正确");
        }

        //4、一致     生成token 保存redis中 返回Result.ok()
        Result result = this.userTokenService.createUserToken(user);

        Map map = new HashMap();
        map.put("token",result.getData());
        map.put("userid",user.getId());
        map.put("name",user.getName());
        return new Result().ok(map);
    }

    @ApiOperation(value = "注册用户接口")
    @PostMapping("/add/user")
    public Result add(@RequestBody UserDto userDto){
        try {
            int i = (int) (Math.random()*900)+100;
            String salt = String.valueOf(i);
            userDto.setSalt(salt);
            Date date = new Date(System.currentTimeMillis());
            userDto.setRegistrationTime(date);
            String pwd = new SimpleHash("MD5", userDto.getPwd(), userDto.getSalt(), 1024).toString();
            userDto.setPwd(pwd);
            this.userService.add(userDto);
            return new Result().ok();
        } catch (Exception e) {
            return new Result().error("注册失败");
        }
    }

    @PutMapping("/update/user")
    @ApiOperation(value = "用户编辑接口")
    public Result update(@RequestBody UserUpdateDto userUpdateDto){
        try {
            this.userService.update(userUpdateDto);
            return new Result().ok();
        } catch (Exception e) {
            return new Result().error("修改失败");
        }
    }

    @GetMapping("/user/queryAccount")
    @ApiOperation(value = "个人中心查询接口")
    public Result queryAccount(Integer userId){
        try {
            UsersDto usersDto1 = this.userService.queryAll(userId);
            return new Result().ok(usersDto1);
        } catch (Exception e) {
            return new Result().error("查询失败");
        }
    }
}
